import java.util.Deque;
import java.util.LinkedList;

/**
 * @author LKQ
 * @date 2021/12/12 16:09
 * @description 删除字符串中的重复项
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.removeDuplicates("abbaca");
    }

    public String removeDuplicates(String s) {
        Deque<Character> deque = new LinkedList<>();
        char[] chars = s.toCharArray();
        int  n = chars.length;
        for (int i = 0; i < n; i++) {
            if (i == 0 || deque.isEmpty()) {
                deque.add(chars[i]);
                continue;
            }
            if (deque.peek() == chars[i]) {
                deque.pop();
            }else {
                deque.push(chars[i]);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (deque.size()!=0){
            sb.append(deque.peek());
            deque.pop();
        }
        return sb.reverse().toString();
    }
}
